<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增字典</title>
    <link rel="stylesheet" href="../../../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../../../admin/css/reset.css"/>
</head>
<body>
<style>
    .mainBox {
        width: auto !important;
    }

    .layui-tab .layui-table-cell {
        overflow: visible !important;
    }

    .layui-table-body, .layui-table-box {
        overflow: visible !important;
    }

    .layui-tab .layui-form-select dl {
        max-height: 190px;
    }

    .layui-table-body .layui-table-col-special:last-child {
        width: 100% !important;
        border-right: 1px solid #eee !important;
    }

    xm-select {
        min-height: 38px;
        line-height: 38px;
    }

    xm-select .xm-body .xm-option .xm-option-icon {
        font-size: 18px !important;
    }
</style>

<form class="layui-form" action="" lay-filter="create-dict-form">

    <div class="mainBox">
        <div class="main-container mr-5">

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label required" style="width:auto">字典名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" required lay-verify="required" autocomplete="off" class="layui-input" placeholder="请输入英文字母组合">
                    </div>
                </div>
            </div>

            <div>

                <!-- 字段属性 -->
                <table id="column-table" lay-filter="column-table"></table>

                <script type="text/html" id="column-toolbar">
                    <button type="button" class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                        <i class="layui-icon layui-icon-add-1"></i>新增
                    </button>
                    <button type="button" class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
                        <i class="layui-icon layui-icon-delete"></i>删除
                    </button>
                </script>

                <script type="text/html" id="col-value">
                    <input type="text" name="value[{{ d.LAY_INDEX-0 }}][value]" placeholder="值" autocomplete="off" class="layui-input" value="{{ d.value }}">
                    <input type="hidden" name="value[{{ d.LAY_INDEX-0 }}][_field_id]" value="{{ d._field_id }}">
                </script>

                <script type="text/html" id="col-name">
                    <input type="text" name="value[{{ d.LAY_INDEX-0 }}][name]" placeholder="标题" autocomplete="off" class="layui-input" value="{{ d.name }}">
                </script>

            </div>


        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                    lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../../component/layui/layui.js"></script>
<script src="../../../../component/pear/pear.js"></script>
<script src="../../../../lms/base64.js"></script>
<script src="../../../../lms/common.js"></script>
<script src="../../../../lms/permission.js"></script>
<script src="../../../../lms/config.js"></script>
<script>


    const PRIMARY_KEY = "id";
    const RESTFUL_API = lms_config.api_url + "/adminapi/system/dict";


    // 字段设置
    layui.use(["table", "common", "popup", "form", "jquery"], function () {

        let table = layui.table;
        let common = layui.common;
        let popup = layui.popup;
        let form = layui.form;
        let method = '';
        let $ = layui.jquery;
        let cols = [
            {
                type: "checkbox",
                width: 50,
            },
            {
                title: "值",
                field: "value",
                templet: "#col-value"
            },
            {
                title: "标题",
                field: "name",
                templet: "#col-name",
            }
        ];

        window._field_id = 0;
        let data = [{
            _field_id: _field_id++,
            value: "",
            name: "",
        }];

        table.render({
            elem: "#column-table",
            cols: [cols],
            data: data,
            cellMinWidth: 40,
            skin: "line",
            size: "lg",
            limit: 10000,
            page: false,
            toolbar: "#column-toolbar",
            defaultToolbar: [],
        });

        if (getQueryString('operation') == 'add') {
            method = 'POST'
        }

        if (getQueryString('operation') == 'edit') {
            method = 'PUT'
            $.ajax({
                url: RESTFUL_API + '/' + getQueryString(PRIMARY_KEY),
                dataType: "json",
                success: function (ret) {
                    if (ret.status == 200) {
                        let options = ret.data.value != undefined ? ret.data.value : [];
                        layui.each(options, function (k, v) {
                            options[k]["_field_id"] = _field_id++;
                        })
                        form.val('create-dict-form', {name: ret.data.name})
                        table.reloadData("column-table", {data: options});
                    }
                }
            });

        }


        table.on("toolbar(column-table)", function (obj) {
            if (obj.event === "add") {
                add();
            } else if (obj.event === "batchRemove") {
                batchRemove(obj);
            }
        });

        let add = function () {
            syncTableData();
            let options = table.getData("column-table");
            options.push({
                _field_id: _field_id++,
                value: "",
                name: "",
            });
            table.reloadData("column-table", {data: options});
        }

        let batchRemove = function (obj) {
            var checkIds = common.checkField(obj, "_field_id");
            if (checkIds === "") return layui.popup.warning("未选中数据");
            let data = table.getData("column-table");
            let newData = [];
            let deleteIds = checkIds.split(",");
            layui.each(data, function (index, item) {
                if (deleteIds.indexOf(item._field_id + "") === -1) {
                    newData.push(item);
                }
            });
            table.reloadData("column-table", {data: newData})
        }


        form.on("submit(save)", function () {
            let data = layui.form.val("create-dict-form");
            layui.$.ajax({
                url: method == 'POST' ? RESTFUL_API : RESTFUL_API + '/' + getQueryString(PRIMARY_KEY),
                type: method,
                dateType: "json",
                data: data,
                success: function (ret) {
                    if (ret.status == 200) {
                        popup.success(ret.msg, function () {
                            parent.refreshTable();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        popup.failure(ret.msg);
                    }
                }
            });
            return false;
        });

        window.syncTableData = function () {
            let tableData = layui.form.val("create-dict-form");
            let columnTableData = [];
            let len = Object.keys(tableData).length;
            let id = 0;
            window._key_id = 0;
            while (id < len) {
                if (typeof tableData["value[" + id + "][_field_id]"] !== "undefined") {
                    columnTableData.push({
                        _field_id: tableData["value[" + id + "][_field_id]"],
                        name: tableData["value[" + id + "][name]"],
                        value: tableData["value[" + id + "][value]"],
                    });
                }
                _key_id++;
                id++;
            }
            layui.table.reloadData("column-table", {data: columnTableData});
        }
    });


</script>

</body>
</html>
